package cn.edu.aust.zhuzhang.mydialogtest.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.lang.ref.WeakReference;

import cn.edu.aust.zhuzhang.mydialogtest.MyApplication;
import cn.edu.aust.zhuzhang.mydialogtest.MyDialog;
import cn.edu.aust.zhuzhang.mydialogtest.R;
import cn.edu.aust.zhuzhang.mydialogtest.utils.FileTool;

public class MainActivity extends AppCompatActivity {
    private EditText et_username=null;
    private EditText et_password=null;
    private Button btn_login=null;

    private static Handler handler=null;

    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_username=(EditText)findViewById(R.id.et_username);
        et_password=(EditText)findViewById(R.id.et_password);
        btn_login=(Button)findViewById(R.id.btn_login);

        MyApplication myApplication = (MyApplication) this.getApplicationContext();
        myApplication.topActivity = this;

        handler=new Handler(){
            //防止Handler引起内存泄露
            private WeakReference<MainActivity> actRef=new WeakReference<MainActivity>(MainActivity.this);
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                String msgStr="";
                if (actRef.get()!=null &&msg.what==0) {
                    et_username.setEnabled(false);
                    et_password.setEnabled(false);
                    btn_login.setEnabled(false);
                }
            }
        };

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username=et_username.getText().toString().trim();
                String pwd=et_password.getText().toString().trim();

                int res= FileTool.checkInSp(MainActivity.this,username,pwd);
                switch(res){
                    case 0:
                        Toast.makeText(MainActivity.this,"登录成功！",Toast.LENGTH_SHORT).show();
                        Intent intent1=new Intent(MainActivity.this,ProgressActivity.class);
                        /*intent1.setAction(Intent.ACTION_VIEW);
                        intent1.setData(Uri.parse("http://www.baidu.com"));*/
                        startActivity(intent1);
                        break;
                    case 1://密码不对
                        MyApplication.count++;
                        //普通对话框
                        AlertDialog.Builder dialogBuilder= new AlertDialog.Builder(MainActivity.this)        //绑定当前界面窗口
                                .setTitle("Dialog对话框") ;
                        if (MyApplication.count>3){
                            MyApplication.count=0;
                                //设置标题
                                dialogBuilder.setMessage("重试已超过3次，退出！")         //设置提示信息
                                    .setIcon(R.mipmap.ic_launcher)       //设置图标
                                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
//                                            Message msg= Message.obtain(handler);
                                            Message msg=new Message();
                                            msg.what=0;
                                            msg.sendToTarget();
                                        }
                                    });
                        }else{
                            String msg="密码错误，只能重试3次，已经重试"+MyApplication.count+"次，要继续重试吗？";
                            dialogBuilder.setMessage(msg)         //设置提示信息
                                    .setIcon(R.mipmap.ic_launcher)       //设置图标
                                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {

                                        }
                                    })     //添加“确定”按钮
                                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            MyApplication.count=0;
                                            Message msg= Message.obtain(handler);
                                            msg.what=0;
                                            msg.sendToTarget();
                                        }
                                    }) ;    //添加“取消”按钮
                        }
                        /*AlertDialog dialog=dialogBuilder.create();                                                          //创建对话框
                        dialog.show();  *///显示对话框
                        dialogBuilder.show();
                        break;
                    case 2://用户名不对
                        MyDialog myDialog = new MyDialog(MainActivity.this, "用户名错误，需要进入注册页面吗？");
                        myDialog.show();
                        break;
                }
            }
        });


        //单选对话框
       /* new AlertDialog.Builder(this)               //生成对话框
                .setTitle("请选择性别")              //设置标题
                .setIcon(R.mipmap.ic_launcher)      //设置图标
                .setSingleChoiceItems(new String[]{"男", "女"}, 0,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        }
                )
                .setPositiveButton("确定", null)
                .show();
*/
        //多选对话框
//        new AlertDialog.Builder(this)
//                .setTitle("请添加兴趣爱好！")
//                .setIcon(R.mipmap.ic_launcher)
//                .setMultiChoiceItems(new String[]{"旅游", "美食", "汽车", "宠物"},
//                        null, null)
//                .setPositiveButton("确定", null)
//                .show();

        //进度条对话框
//        ProgressDialog prodialog;                //声明对话框
//        prodialog = new ProgressDialog(this); //构建对话框
//        prodialog.setTitle("进度条对话框");
//        prodialog.setIcon(R.mipmap.ic_launcher);
//        prodialog.setMessage("正在下载请等候...");
//        prodialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //设置水平进度条
////        prodialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//设置圆形进度条
//        prodialog.show();


        //创建Toast
//         Toast.makeText(MainActivity.this, "Hello,Toast", Toast.LENGTH_SHORT).show();

        //自定义对话框
       /* MyDialog myDialog = new MyDialog(this, "我是自定义的Dialog");
        myDialog.show();*/

    }
}
